



_myorder = {

    getOrderList:function (orderId,keyword,pageSize,pageNum,orderSource,orderState,deliveryStatus) {

        $.ajax({
            type        :   'GET',
            url         :   _common_util.getServerURL('order/customer_order_list'),
            dataType    :   'json',
            data        :   {
                'orderId'       :   orderId,
                'keyword'       :   keyword,
                'pageSize'      :   pageSize,
                'pageNum'       :   pageNum,
                'orderSource'   :   orderSource,
                'orderState'    :   orderState,
                'deliveryStatus':   deliveryStatus
            },
            success:function (res) {
                console.log(res);
                if (res.status  !==  0){
                    console.log('getOrderList error');
                }else {

                    let orderstr ='';
                    $('#order-content').html('');

                    $.each( res.data.customerOrderVOList, function (i,item) {

                        let imgsrc =_common_util.getImageUrl(item.img);

                        let oState= "";
                        if (item.orderState === 0 ){
                            //未支付
                            oState = ' <a href="#" style="margin-top: 10px" >未支付</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'" style="display: block;margin-top: 10px" class="btn-info">订单详情</a>\n' +
                                '      <a href="order.html?orderId='+item.orderId+'" style="margin-top: 10px" class="btn-info" >前往支付</a>'
                        }else if (item.orderState === 1){
                            //订单取消
                            oState = ' <a href="#" style="margin-top: 10px" >订单取消</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'" style="display: block;margin-top: 10px" class="btn-info">订单详情</a>'
                        }else if (item.orderState === 2){
                            //已支付待收货
                            oState = ' <a href="#" style="margin-top: 10px" >已支付</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'" style="display: block;margin-top: 10px" class="btn-info">订单详情</a>\n' +
                                '       <a href="#" onclick="confirm('+item.orderId+')" style="display: block;margin-top: 10px" class="btn-info">确认收货</a>'
                        }else if (item.orderState === 3){
                            //已收货待评价
                            oState = ' <a href="#" style="margin-top: 10px" >已收货</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'" style="display: block;margin-top: 10px" class="btn-info">订单详情</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'&commentflag=1" style="margin-top: 10px" class="btn-info" >追加评论</a>'
                        }else if (item.orderState === 4){
                            oState = ' <a href="#" style="margin-top: 10px" >已收货</a>\n' +
                                '      <a href="order-info.html?orderId='+item.orderId+'" style="display: block;margin-top: 10px" class="btn-info">订单详情</a>\n' +
                                '      <a href="###" style="margin-top: 10px" class="btn-info" >已评价</a>'
                        }

                        orderstr = '                            <table class="table" style="display: inline-block;width:100%" >\n' +
                            '                                <tbody>\n' +
                            '                                <tr style="background-color: #fed700">\n' +
                            '                                    <td style="width: 450px">'+item.createTime+' id: <a href="#" class="order-id">'+item.orderId+'</a></td>\n' +
                            '                                    <td></td>\n' +
                            '                                    <td></td>\n' +
                            '                                    <td></td>\n' +
                            '                                    <td></td>\n' +
                            '                                    <td>x</td>\n' +
                            '                                </tr>\n' +
                            '                                </tbody>\n' +
                            '                                <tbody style="border:1px solid #f0ad4e">\n' +
                            '                                <tr class="active">\n' +
                            '                                    <td class="row">\n' +
                            '                                             <span style="float: left" >\n' +
                            '                                                 <img src="'+imgsrc+'" width="100px" height="100px">\n' +
                            '                                             </span>\n' +
                            '                                        <div class="col-lg-6">\n' +
                            '                                            <div> <a href="#">'+item.itemName+'</a></div>\n' +
                            '                                            <div>'+item.caddress+'</div>\n' +
                            '\n' +
                            '                                        </div>\n' +
                            '                                    </td>\n' +
                            '\n' +
                            '                                    <td><span>'+item.unitPrice+'</span>¥</td>\n' +
                            '                                    <td><span>'+item.itemNumber+'</span></td>\n' +
                            '                                    <td><span>'+item.totalPrice+'</span>¥</td>\n' +
                            '                                    <td>\n' +

                                                                     oState +

                            '                                    </td>\n' +
                            '                                    <td></td>\n' +
                            '                                </tr>\n' +
                            '                                </tbody>\n' +
                            '\n' +
                            '                            </table>\n'


                        $('#order-content').append(orderstr);
                    })


                    totalpage = res.data.endRow;
                    currentpage = res.data.pageNum;
                    prepage = res.data.prePage;
                    nextpage = res.data.nextPage;
                    restpage = totalpage - currentpage;
                    console.log("剩余的页数："+restpage);
                    let pagestr='';

                    if (currentpage>3){
                        //1和省略号
                        pagestr = pagestr+'               <li><a onclick="toPage(this)" page="1" class="page-numbers">1</a></li> \n' +
                            '                              <li class="apostrophe">...</li>' +
                            '                             <li><a onclick="toPage(this)" page="'+prepage+'" class="page-numbers">'+prepage+'</a></li>';
                    }else {
                        for (var i = 1; i<currentpage;i++) {
                            pagestr = pagestr + '                <li><a onclick="toPage(this)" page="' + i + '" class="page-numbers">' + i + '</a></li>';
                        }
                    }
                    pagestr = pagestr + '                   <li><a onclick="toPage(this)" page="'+currentpage+'" class="page-numbers current">'+currentpage+'</a></li>\n';
                    if (restpage>2){
                        pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li>       ' +
                            '               <li class="apostrophe">...</li>\n' +
                            '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                    }else if (nextpage != totalpage && res.data.hasNextPage) {

                        pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li> ' +
                            '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li> ';
                    }else if (nextpage === totalpage){
                        pagestr = pagestr + ' <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                    }
                    $('#page-numbers').html(pagestr);

                }
            }
        })
    }
    
    
}


function toPage(item){
    let page =$(item).attr('page');
    console.log('cccccpage:'+page);
    _myorder.getOrderList('','','',page,'','','');
}


function confirm(orderId){

    window.wxc.xcConfirm('是否确认收货?','confirm',{onOk:function () {
            $.ajax({
                type: 'POST',
                url : _common_util.getServerURL('order/order_confirm'),
                dataType: 'json',
                data    :   {
                    'orderId' : orderId
                },
                success : function (res) {
                    console.log('confirm'+res)
                    if (res.status === 0 ){
                        console.log('收获成功');
                        _myorder.getOrderList('','','','','','2','');
                    }
                }
            })
        }})


}


$(function () {

    console.log('init');
    _myorder.getOrderList();
    $('.order-all').on('click',function () {
        console.log('findall');
        _myorder.getOrderList();
    })

    $('.order-paid').on('click',function () {
        console.log('已支付订单');
        _myorder.getOrderList('','','','','','2','');
    })

    $('.order-none-paid').on('click',function () {
        console.log('未支付订单');
        _myorder.getOrderList('','','','','','0','');
    })
    $('.order-none-appraise').on('click',function () {
        console.log('未评价订单');
        _myorder.getOrderList('','','','','','3','');
    })


})